//
//  RoomListViewController.m
//  Charles
//
//  Created by Charles on 2018/5/13.
//  Copyright © 2018年 charles. All rights reserved.
//

#import "RoomListViewController.h"
#import "RoomListViewCell.h"
#import "RoomDetailViewController.h"
#import "CLCollectionViewController.h"
#import "CLHomeViewTransition.h"

@interface RoomListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) CLHomeViewTransition *transition;

@end

@implementation RoomListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"房源列表";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(20, 40, 10, 10);
    [button addTarget:self action:@selector(didTapBackButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    [button setBackgroundImage :[UIImage imageNamed:@"back"] forState:UIControlStateNormal];

    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(didTapBackButton)];
    self.navigationItem.leftBarButtonItem = leftBar;
}

- (void)didTapBackButton {
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 8;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 220;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *strId =@"Id";
    RoomListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strId];
    if (cell == nil) {
        cell = [[RoomListViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strId];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    __weak typeof(self) weakSelf = self;
    
    cell.selectBlock = ^(NSIndexPath *indexPath,UICollectionView *collection , RoomListViewCell *cell){
        [weakSelf pushViewController:indexPath collcetionView:collection forCell:cell];
    };
    
    return cell;
}

- (void)pushViewController:(NSIndexPath *)indexPath collcetionView:(UICollectionView *)collectionView forCell:(RoomListViewCell *)cell {
    RoomListCollectionCell *collectionCell = (RoomListCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    RoomDetailViewController *toViewController = [[RoomDetailViewController alloc]init];
    toViewController.coverImage = collectionCell.converImageView.image;
    
    toViewController.closeBlock = [self.transition transitionFromViewController:self toViewController:toViewController selectedIndexPath:indexPath collectionView:collectionView completion:toViewController.fadeBlock];
}

- (CLHomeViewTransition *)transition {
    if (nil == _transition) {
        _transition = [CLHomeViewTransition new];
    }
    return _transition;
}

- (UITableView *)tableView {
    if (nil == _tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
